﻿using System.Data.Objects.SqlClient;
using System.Linq;
using Pyramid.Models;
using Pyramid.Security;

namespace Pyramid.Bll
{
	public class MemberCenterHelper : BllBase
	{
		public static MemberCenterHelper NewInstance
		{
			get { return new MemberCenterHelper(); }
		}

		public IQueryable<MemberCenter> GetList(MemberQueryModel model, CustomPrincipal user)
		{
			var members = Db.MemberCenters.Where(x => x.CreateUserId == user.UserId);
			if (!string.IsNullOrWhiteSpace(model.MemberCode))
			{
				members = members.Where(x => x.Code.Contains(model.MemberCode));
			}
			if (!string.IsNullOrWhiteSpace(model.Keywords))
			{
				members = members.Where(x => x.Name.Contains(model.Keywords));
			}
			if (model.EstablishDateFrom.HasValue)
			{
				members = members.Where(x => SqlFunctions.DateDiff("d", model.EstablishDateFrom.Value, x.EstablishDate) >= 0);
			}
			if (model.EstablishDateTo.HasValue)
			{
				members = members.Where(x => SqlFunctions.DateDiff("d", model.EstablishDateTo.Value, x.EstablishDate) <= 0);
			}
			return members;
		}

		public MemberCenter GetMemberCenter(int memberCenterId)
		{
			return Db.MemberCenters.Single(x => x.Id == memberCenterId);
		}
	}
}